#coding=utf-8
from django.contrib.auth.models import User, Group
from django.shortcuts import render_to_response
from django.template import RequestContext
from DjangoBbs.Users import models as UsersModel


__author__ = 'czq'
class ManageView:
  def list(self,request):
      dict={}
      menuList=(
          ('?action=updateUserProfile','更新用户扩展','修复不存在的用户扩展表,防止出现问题'),
          ('?action=updateUserCount','主题数量','更新用户的主题和回复数量'),
      )
      dict["menuList"]=menuList
      action=request.GET.get('action','')
      if action!='':
          if action=='updateUserProfile':
              group=Group.objects.all()[0]
              userlist=User.objects.all()
              for item in userlist:
                  try:
                      item.get_profile()
                  except:
                      profile=UsersModel.UserProfile(user=item,group=group,nickName=u'nickName')
                      profile.save()
                  try:
                      UsersModel.UserInfo.objects.get(user=item)
                  except:
                      user=UsersModel.UserInfo(user=item)
                      user.save()
                  try:
                      UsersModel.UserSetting.objects.get(user=item)
                  except:
                      user=UsersModel.UserSetting(user=item)
                      user.save()
              dict['info']=u'更新成功</br>'
          elif action=="updateUserCount":
              from PublicTool.DbHelper import ExecuteSql
              ExecuteSql("update Users_userprofile set themeCount=(select count(1) from BbsTheme_bbstheme where isDel=0 and addUser_id=Users_userprofile.user_id),replyCount=(select count(1) from BbsTheme_bbsreply where isDel=0 and addUser_id=Users_userprofile.user_id)")
              dict['info']=u'统计更新成功</br>'
      return render_to_response("Manage/manage_list.html",dict,context_instance=RequestContext(request))